/**
 * Created by William Wang on 13-8-26.
 */
package com.sunose.agfmk.view;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import com.sunose.agfmk.constant.Constant;
import com.sunose.agfmk.R;

public class GameView extends SurfaceView implements Callback {

    public static final String tag = Constant.TAG_PREFIX+"_gameview";
    //声明GameThread类实例
    GameThread gameThread;
    Context parent=null;
    public GameView(Context context) {
        super(context);
        init(context);
        Log.d(tag,"call context");

    }
    public void init(Context context)
    {
        parent=context;//keep
        SurfaceHolder surfaceHolder = getHolder();
        //添加回调对象
        surfaceHolder.addCallback(this);
        //创建GameThread类实例
        gameThread = new GameThread(surfaceHolder);
    }
    public GameView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        Log.d(tag,"call Context context, AttributeSet attrs, int defStyle");
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        Log.d(tag,"call Context context, AttributeSet attrs");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawText("Hello, World", 10,10,new Paint() );
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.v(tag, "surfaceCreated");
        gameThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
        Log.v(tag, "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.v(tag, "surfaceDestroyed");
        //通过结束run()函数的方法结束gameThread，详见GameThread类的定义
        gameThread.run = false;
    }
    class GameThread extends Thread {
        SurfaceHolder surfaceHolder;
        //run()函数中控制循环的参数。
        boolean run = true;
        public GameThread(SurfaceHolder surfaceHolder) {
            this.surfaceHolder = surfaceHolder;
        }
        int i = 0;
        int mWidth=800;
        int mHeight=480;
        Bitmap mDrawing = BitmapFactory.decodeResource(parent.getResources(),R.drawable.gi_background);// Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        Bitmap log = BitmapFactory.decodeResource(parent.getResources(),R.drawable.ic_launcher);
        float scale = parent.getResources().getDisplayMetrics().density;
        android.graphics.Bitmap.Config bitmapConfig =
                mDrawing.getConfig();

        Paint fontsize=new Paint();

        Matrix matrix = new Matrix();//rotate

        Canvas c = null;
        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(bitmapConfig == null) {
                bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
            }
            fontsize.setColor(Color.rgb(61, 61, 61));
            fontsize.setTextSize(36.0f);
            fontsize.setShadowLayer(1f, 0f, 1f, Color.WHITE);
            fontsize.setAlpha(100);// transparent
            fontsize.setAntiAlias(true);
            //       fontsize.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));

            int angle=0;
            mDrawing = mDrawing.copy(bitmapConfig, true);
            Canvas tempCanvas = new Canvas();
            tempCanvas.drawBitmap(mDrawing, null,
                    new Rect(0, 0, mWidth, mHeight), new Paint());
            while(run) {
                Log.v(tag, "GameThread");

                try {
                    synchronized (surfaceHolder) {
                        //我们在屏幕上显示一个计数器，每隔1秒钟刷新一次
                        c = surfaceHolder.lockCanvas();//draw on back buffer
                        if(i>=0)
                        {
                        c.drawBitmap(mDrawing, null,
                                new Rect(0, 0, mWidth, mHeight), new Paint());
                        }
                       // c.drawText("" + i++, 10+i, 10+i, fontsize);
                        angle=(i++)*10;

                        c.rotate(angle,36,36); //key statemnt
                        Log.v(tag,"angle:"+angle);
                        c.drawBitmap(log, matrix, null); //draw the ball with the applied matrix
                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    if (c != null) {
                        surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}
